package org.stars.daostars.sqlmapper.loader.xml;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.stars.daostars.exceptions.parser.MalformedVariableException;
import org.stars.daostars.sqlmapper.SqlMapper;
import org.stars.util.StringUtility;

import org.xml.sax.Attributes;

/**
 * Gestore del tag variable.
 * 
 * @author Francesco Benincasa (908099)
 *
 */
public abstract class VariableTagHandler {
	private static final String NAME = "name";
	private static final String VALUE = "value";
	

	protected static Log getLog()
	{
		return LogFactory.getLog(VariableTagHandler.class);
	}
	
	public static SqlMapper startElement(SqlMapper definition, Attributes attributes) throws MalformedVariableException	
	{		
		String sName = StringUtility.nvl(attributes.getValue(NAME)).toUpperCase();
		String sValue = StringUtility.nvl(attributes.getValue(VALUE));

		if (sName.length() == 0) {
			Log logger=getLog();
			String msg = "Dao " + definition.getName() + " contains a variable without name.";
			definition.addParserError(msg, MalformedVariableException.class);
			logger.error(msg);
			throw(new MalformedVariableException(msg));
		} else {
			definition.setVariable(sName, sValue);
		}
		
		return definition;
	}
}
